package org.dromara.system.controller.device;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.DeviceGroupBo;
import org.dromara.system.domain.vo.DeviceGroupVo;
import org.dromara.system.service.IDeviceGroupService;
import org.dromara.system.service.IDeviceUserGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备分类
 *
 * @author Lion Li
 * @date 2024-10-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/group")
public class DeviceGroupController extends BaseController {

    private final IDeviceGroupService deviceGroupService;

    private final IDeviceUserGroupService groupService;

    /**
     * 获取设备所有分类
     */
    @SaCheckPermission("system:device:list")
    @GetMapping("/list")
    public R<List<DeviceGroupVo>> list(DeviceGroupBo bo) {
        Long loginUser = LoginHelper.getUserId();
        if (loginUser==1L){
            return R.ok(deviceGroupService.getGroupList(bo));
        }else {
            return R.ok(groupService.getGroupList(loginUser));
        }
    }

    @SaIgnore
    @GetMapping("/getAllGroup")
    public R<List<DeviceGroupVo>> getAllGroup() {
        return R.ok(deviceGroupService.getAllGroup());
    }


    /**
     * 获取设备分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:device:list")
    @GetMapping("/{id}")
    public R<DeviceGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(deviceGroupService.queryById(id));
    }

    /**
     * 新增设备分类
     */
    @SaCheckPermission("system:device:add")
    @Log(title = "设备分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceGroupBo bo) {
        return toAjax(deviceGroupService.insertByBo(bo));
    }

    /**
     * 修改设备分类
     */
    @SaCheckPermission("system:device:edit")
    @Log(title = "设备分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceGroupBo bo) {
        return toAjax(deviceGroupService.updateByBo(bo));
    }

    /**
     * 删除设备分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:device:remove")
    @Log(title = "设备分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(deviceGroupService.deleteWithValidByIds(List.of(ids), true));
    }
}
